package cn.zhaoyuening.crm.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import cn.zhaoyuening.crm.constant.PathConstant;
import cn.zhaoyuening.crm.domain.CrmClass;
import cn.zhaoyuening.crm.service.ClassService;
import cn.zhaoyuening.crm.utils.MyStreamUtils;

public class ClassAction extends BaseAction<CrmClass> {
	private ClassService classService;
	private CrmClass clazz;
	
	private File classTable;				//上传内容
	private String classTableFileName;	//上传文件名
	private String classTableContentType;	//上传类型

	public String list(){
		//获取所有班级
		List<CrmClass> list = classService.findAll();
		putContext("allClass", list);
		return "list";
	}
	/**
	 * 跳转到上传课表ui界面
	 * @return
	 */
	public String uploadClassTableUi(){
		this.clazz = this.classService.findById(this.clazz.getClassId());
		return "uploadClassTableUi";
	}
	/**
	 * 上传课程表
	 * @return
	 * @throws IOException 
	 */
	public String uploadClassTable() throws IOException{
		//保存路径
		String savePath = ServletActionContext.getServletContext().getRealPath(PathConstant.CLASS_TABLE_SAVE_PATH);
		
		//保存文件名
		//设置一个随机码，不保存扩展名保证安全性
		String saveFileName = UUID.randomUUID().toString().replace("-","");
		this.clazz.setUploadFilename(classTableFileName);
		this.clazz.setUploadPath(savePath+File.separator+saveFileName);
		this.clazz.setUploadTime(new Timestamp(new Date().getTime()));
		
		this.classService.uploadClassTable(classTable,clazz);
		
		addActionMessage("Add class table success!");
		//回退到班级列表
		return list();
	}
	
	public InputStream getClassTableFile() throws IOException{
		CrmClass crmClass = classService.findById(clazz);
		//如果没有上传
		if (StringUtils.isBlank(crmClass.getUploadPath())) {
			addActionMessage("Not have class table!");
			return null;
		}
		
		
		//无效文件
		if (new File(crmClass.getUploadPath()).isFile()) {
			addActionMessage("Not have class table!");
			return null;
		}
		//显示给用户的用户名
		String fileName = crmClass.getUploadFilename();
		setClassTableFileName(fileName);
		return ServletActionContext.getServletContext()
				.getResourceAsStream(crmClass.getUploadPath());
	}
	public ClassService getClassService() {
		return classService;
	}

	public void setClassService(ClassService classService) {
		this.classService = classService;
	}

	public CrmClass getClazz() {
		return clazz;
	}

	public void setClazz(CrmClass clazz) {
		this.clazz = clazz;
	}

	public File getClassTable() {
		return classTable;
	}

	public void setClassTable(File classTable) {
		this.classTable = classTable;
	}

	public String getClassTableFileName() {
		return classTableFileName;
	}

	public void setClassTableFileName(String classTableFileName) {
		this.classTableFileName = classTableFileName;
	}

	public String getClassTableContentType() {
		return classTableContentType;
	}

	public void setClassTableContentType(String classTableContentType) {
		this.classTableContentType = classTableContentType;
	}
	
	
	
}
